<?php
/**
 * Description of SQMCDimClass
 *
 * Sub Question Multiple Choice Dimension
 *
 * @author Chisheng Lu, Jeffy shih
 * @copyright CopyRight(c)DoRISE team, Science Education center, NTNU.
 * @since 2010-03-09
 * @version 1.0
 */
class SQMCDim {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    // $arySQMCDim: Array
    // Returns Boolean
    public function addSQMCDim($intSQId, $strDimCode) {
        $bolRtn = false;
        $sql_addSQMCD0 = 'select count(*) from SQMCDim where SQId = ' . $intSQId . ';';
        $rs_addSQMCD0 = $this->mysqli->query($sql_addSQMCD0);
        if ($rs_addSQMCD0) {
            $row_getSQMCD0 = $rs_addSQMCD0->fetch_row();
            if ($row_getSQMCD0[0] > 1) {
                throw new Exception('新增向度資料時發生錯誤，向度資料筆數過多！');
            }
        }

        $strNow = $this->getNowTime();
        $sql_addSQMCD = 'INSERT INTO `SQMCDim` (`SQId`, `DimCode`) VALUES(' . $intSQId . ', \'' . $strDimCode . '\')';
        $rs_addSQMCD = $this->mysqli->query($sql_addSQMCD);
        if (!$rs_addSQMCD) {
            throw new Exception('新增向度資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;

    }

    // $intSQMCDId: int
    // $arySQMCDim: Array
    // Returns Boolean
    public function editSQMCDim($intSQMCDId, $arySQMCDim) {
        //檢查題幹編號是否為空白
        if (empty ($intSQMCDId)) {
            throw new Exception('缺少上傳向度編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editSQMCD = 'Update SQMCDim set ';
        $bolFirst = true;
        if (array_key_exists('SQId', $arySQMCDim)) {
            if ($bolFirst == false) {
                $sql_editSQMCD .= ', ';
            }
            $sql_editSQMCD .= 'SQId = ' . $arySQMCDim['SQId'] . ' ';
            $bolFirst = false;
        }
        if (array_key_exists('DimCode', $arySQMCDim)) {
             if ($bolFirst == false) {
                $sql_editSQMCD .= ', ';
            }
            $sql_editSQMCD .= 'DimCode = \'' . $arySQMCDim['DimCode'] . '\' ';
            $bolFirst = false;
        }
        $rs_editSQMCD = $this->mysqli->query($sql_editSQMCD);
        if (!$rs_editSQMCD) {
            throw new Exception('更新上傳向度資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;
    }

    public function delSQMCDimBySQId($intSQId) {
        $bolRtn = false;
        //檢查上傳圖檔編號是否為空白
        if (empty ($intSQId)) {
            throw new Exception('錯誤！缺少子題編號，無法刪除向度內容。');
        }

        //刪除
        $sql_delSQMCD = 'DELETE FROM `SQMCDim` WHERE `SQId`=' . $intSQId;
        $rs_delSQMCD = $this->mysqli->query($sql_delSQMCD);
        if (!$rs_delSQMCD) {
            throw new Exception('錯誤！刪除向度時發生錯誤。');
        }else{
            $bolRtn = true;
        }

        return $bolRtn;

    }

    // $intSQMCDId: int
    // Returns Boolean
    public function delSQMCDimBySQMCDId($intSQMCDId) {
        $bolRtn = false;
        //檢查上傳圖檔編號是否為空白
        if (empty ($intSQMCDId)) {
            throw new Exception('錯誤！缺少向度編號。');
        }

        //刪除
        $sql_delSQMCD = 'DELETE FROM `SQMCDim` WHERE `SQMCDId`=' . $intSQMCDId;
        $rs_delSQMCD = $this->mysqli->query($sql_delSQMCD);
        if (!$rs_delSQMCD) {

            throw new Exception('錯誤！刪除向度時發生錯誤。');
        }else{
            $bolRtn = true;
        }

        return $bolRtn;

    }

    public function getSQMCDimAndLevelNameBySQId($intSQId) {
        if (empty($intSQId)) {
            throw new Exception('缺少子題編號，無法查詢向度資料！');
        }

        $aryDAndLName = array();

        $sql_selDimCode = 'SELECT `DimCode` FROM `SQMCDim` WHERE `SQId`=' . $intSQId . ' ORDER BY `SQMCDId`';
        $rs_selDimCode = $this->mysqli->query($sql_selDimCode);
        if(!$rs_selDimCode){
            throw new Exception('查詢向度編號資料時發生錯誤！');
        }else{
            while ($row_selDimCode = $rs_selDimCode->fetch_row()){
                $strDimCode = $row_selDimCode[0];
                $intSDId = intval(substr($strDimCode, 0, 2));
                $intSDLOrder = intval(substr($strDimCode, 2, 2));

                $sql_getDimName = 'SELECT `SDChtName` FROM `SDimension` WHERE `SDId`=' . $intSDId;
                $rs_getDimName = $this->mysqli->query($sql_getDimName);
                $row_getDimName = $rs_getDimName->fetch_row();
                
                $sql_getLevelName = 'SELECT `SDLChtName` FROM `SDimLevel` WHERE `SDId`=' . $intSDId . ' AND `SDLOrder`=' . $intSDLOrder;
                $rs_getLevelName = $this->mysqli->query($sql_getLevelName);
                $row_getLevelName = $rs_getLevelName->fetch_row();
                
                $strDimAndLevelName = $row_getDimName[0] . '-' . $row_getLevelName[0];
                
                array_push($aryDAndLName, $strDimAndLevelName);
                
                //釋放查詢結果
                $rs_getDimName->free_result();
                $rs_getLevelName->free_result();

            }
            $rs_selDimCode->free_result();
        }
        
        return $aryDAndLName;

    }

    public function getSQMCDimCodeBySQId($intSQId){
        if (empty($intSQId)) {
            throw new Exception('缺少子題編號，無法查詢向度編號資料！');
        }

        $aryDimCode = array();

        $sql_getDimCode = 'SELECT `DimCode` FROM `SQMCDim` WHERE `SQId`=' . $intSQId . ' ORDER BY `SQMCDId`';
        $rs_getDimCode = $this->mysqli->query($sql_getDimCode);

        if(!$rs_getDimCode){
            throw new Exception('查詢向度編號資料時發生錯誤！');
        }else{
            while ($row_getDimCode = $rs_getDimCode->fetch_row()){
                array_push($aryDimCode, $row_getDimCode[0]);
            }
        }

        return $aryDimCode;
    }

    // $intSQMCDId: int
    // Returns Array
    public function getSQMCDimInfoBySQMCDId($intSQMCDId) {
        //檢查上傳圖檔編號是否為空白
        if (empty ($intSQMCDId)) {
            throw new Exception('缺少子題選擇題向度編號！');
        }

        $sql_getSQMCDById = 'SELECT * FROM `SQMCDim` WHERE `SQMCDId`=' . $intSQMCDId;
        $rs_getSQMCDById = $this->mysqli->query($sql_getSQMCDById);

        if (!$rs_getSQMCDById) {
            throw new Exception('無法查詢該子題選擇題向度的資料！');
        }else{
            if ($rs_getSQMCDById->num_rows == 0) {
                throw new Exception('無此子題選擇題向度編號的資料！');
            }else{
                $row_getSQMCDById = $rs_getSQMCDById->fetch_row();
                        return $row_getSQMCDById;
            }
        } //End If

    }

    // Returns Array
    public function getSQMCDimList() {
        $sql_getSQMCDList = 'SELECT * FROM `SQMCDim` ;';
        $rs_getSQMCDList = $this->mysqli->query($sql_getSQMCDList);

        if (!$rs_getSQMCDList) {
            throw new Exception('無法查詢該子題選擇題向度的資料！');
        }else{
            if ($rs_getSQMCDList->num_rows == 0) {
                throw new Exception('無此子題選擇題向度編號的資料！');
            }else{
                $arySQMCDList = array();
                while ($aryOneSQMCD = $rs_getSQMCDList->fetch_row()) {
                    array_push($arySQMCDList, $aryOneSQMCD);
                }
                return $arySQMCDList;
            }
        } //End If

    }

    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
